#include <REGX52.H>
#include "uart.c"
#include "timer.c"
#include "game.h"
#include "display.c"
/**
 * 实现形状显示
 */
unsigned char type = 1;
unsigned char direction = 0;
unsigned int shape_data;

sbit BTN_UP = P1 ^ 2;
sbit BTN_DOWN = P1 ^ 3;
sbit BTN_LEFT = P1 ^ 4;
sbit BTN_RIGHT = P1 ^ 5;

struct
{
    // TETRIS_SHAPES二维数组的两个维度
    unsigned char shape_type; // 方块的形状类型 0-6
    unsigned char shape_pos;  // 方块的方位 0-3
    // 位于游戏区域的坐标
    unsigned char x;         // 水平方位 0~9
    unsigned char y;         // 竖直的方位  0~15
    unsigned int shape;      // 形状数据 TETRIS_SHAPES中的一条数据
    unsigned char next_type; // 下一个方块的类型
    unsigned char next_pos;  // 下一个方块的方位
} game_shape;

// 十六个元素，每个代表一行，每行10列，
// 0010 0000 0000 0100 ,中间10位0，每位代表一个4x4像素的格子
unsigned int code GAME_ZONE_INIT_DATA[] = {
    0x2004, 0x2004, 0x2004, 0x2004,
    0x2004, 0x2004, 0x2004, 0x2004, //
    0x2004, 0x2004, 0x2004, 0x2004,
    0x2004, 0x2004, 0x2004, 0x2004,
    // 多出来3行用来防止数组越界，保持处理逻辑一致
    // 形状是4x4方格，最左上角是坐标原点，允许坐标落到游戏区域的最底端，这时候向下伸出了3行
    0xFFFF, 0xFFFF, 0xFFFF};

unsigned int idata GAME_ZONE[19];

void game_zone_init()
{
    unsigned char i;
    for (i = 0; i < 19; i++)
    {
        GAME_ZONE[i] = GAME_ZONE_INIT_DATA[i];
    }
}

void display_game_zone()
{
    unsigned char i, j;
    bit top;
    bit bottom;
    unsigned int temp;
    unsigned char display_byte[] = {0, 0, 0, 0};

    // 16行数据，每2行数据同时写入，一共做8次
    // i刚好是页数
    for (i = 0; i < 8; i++)
    {
        // 每行10列，1列显示4byte数据
        for (j = 0; j < 10; j++)
        {
            // xx10 0000 0000 01xx
            // GAME_ZONE[i]
            temp = GAME_ZONE[i * 2];
            top = ((temp >> (12 - j)) & 0x0001) == 1;
            temp = GAME_ZONE[i * 2 + 1];
            bottom = ((temp >> (12 - j)) & 0x0001) == 1;
            fill_display_block_arr(top, bottom, display_byte);
            // 30是竖线
            lcd_display_bytes_in_page(i, 31 + 4 * j, display_byte, 4);
            // GAME_ZONE[i+1]
        }
    }
}

#define GAME_FRAME_TOTAL 2

unsigned char game_frame_count = 0;

// 初始化game_shape
void game_init_shape()
{
    game_shape.shape_type = timer_rand() % 7;
    game_shape.shape_pos = timer_rand() % 4;
    game_shape.x = 3;
    game_shape.y = 0;
    game_shape.shape = TETRIS_SHAPES[game_shape.shape_type][game_shape.shape_pos];
    game_shape.next_type = timer_rand() % 7;
    game_shape.next_pos = timer_rand() % 4;
}

void add_shape_to_game_zone()
{
    // xxxx xxxx xxxx xxxx
    // xxxx
    // xxxx
    // xxxx
    // xxxx

    // 根据game_shpe的y坐标，修改game_zone[y~y+4] 4组数据
    unsigned char i;
    unsigned int game_zone_val;
    unsigned int temp_shape_data;
    for (i = 0; i < 4; i++)
    {
        game_zone_val = GAME_ZONE[game_shape.y + i];
        temp_shape_data = game_shape.shape;

        // game_shape.shape 每次循环，取出其中的4位修改到game_zone
        // xx1??????????1xx
        //    ↑↑↑↑
        // 最多从低位移动9位，用于和游戏区域的最左侧对齐
        game_zone_val |= ((temp_shape_data >> (12 - 4 * i)) & 0x000F) << (9 - game_shape.x);
        GAME_ZONE[game_shape.y + i] = game_zone_val;

        // GAME_ZONE[game_shape.y + i] |= ((game_shape.shape >> (12 - 4 * i)) & 0x000F) << (9 - game_shape.x);
    }
}

void remove_shape_from_game_zone()
{
    unsigned char i;
    unsigned int game_zone_val;
    unsigned int temp_shape_data;
    for (i = 0; i < 4; i++)
    {
        game_zone_val = GAME_ZONE[game_shape.y + i];
        temp_shape_data = game_shape.shape;
        game_zone_val &= ~((temp_shape_data >> (12 - 4 * i)) & 0x000F) << (9 - game_shape.x);
        GAME_ZONE[game_shape.y + i] = game_zone_val;
    }
}

// 生成下一个方块
void next_game_shape()
{
    game_shape.shape_type = game_shape.next_type;
    game_shape.shape_pos = game_shape.next_pos;
    game_shape.x = 3;
    game_shape.y = 0;
    game_shape.shape = TETRIS_SHAPES[game_shape.shape_type][game_shape.shape_pos];
    game_shape.next_type = timer_rand() % 7;
    game_shape.next_pos = timer_rand() % 4;
}

void timer0_ISR() interrupt 1
{
    if (game_frame_count < GAME_FRAME_TOTAL)
    {
        game_frame_count++;
    }
    else
    {
        game_frame_count = 0;
        // 将方块写入到game_zone

        display_game_zone();
    }
}

void main()
{
    uart_init();
    timer0_init();
    display_init();
    game_zone_init();
    // GAME_ZONE[0] = 0x2F04;
    // GAME_ZONE[4] = 0x3FFC;
    game_init_shape();
    add_shape_to_game_zone();
    timer0_ISR_enable();
    while (1)
    {
        if (BTN_UP == 0)
        {
            while (BTN_UP == 0)
            {
            }
            // 对形状操作的时候，停掉中断
            timer0_ISR_disable();
            remove_shape_from_game_zone();
            next_game_shape();
            add_shape_to_game_zone();
            display_next_shape(TETRIS_SHAPES[game_shape.next_type][game_shape.next_pos]);
        }
    }
}